/* **************************************************************************
 *
 **************************************************************************/

#include "pvoutput.h"

/****************************************************************************************
 *				       			--- Global Parameters ---			                    *
 ***************************************************************************************/

static TCP_SOCKET Socket;
static BOOL flagTCP=FALSE;
static BOOL flagTCPisCON=FALSE;
static int z = 0;
static BYTE error = 0;

  /*-------------------------------------------------------------------------------------
  |	Function: 		wPVOutput(char* systemId, char* apikey, char* data)		  		 		|
  | Description: 	Function to write float data in a specific pvoutput database.		|
  | Returns:		BYTE 0 - No error										   			|
  | 		  			 1 - TCPClientOpen error							   			|
  | Parameters:		char* apikey - ThingSpeak write api key				 				|
  | 																					|
  |					int* value - array with float data to send to ThingSpeak    	    |
  |																						|
  |					int nvalue - number of value elements							    |
  -------------------------------------------------------------------------------------*/

BYTE wPVOutput(char* systemId, char* apikey, char* data)
{
	error = 0;
	flagTCP=0;
	
	//UARTWrite(1, "\n wPVoutput started \n ");

	
	char strPOST[270+(int)strlen(data)];
	// orig : sprintf( strPOST, "POST /update HTTP/1.1\r\nHost: api.thingspeak.com\r\nConnection: close\r\nX-THINGSPEAKAPIKEY: %s\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: %d\r\n\r\n%s", apikey, (int)strlen(messageTS), messageTS);
	
	// DIT WERKT
	//"data=20121016,15000"
	//sprintf( strPOST, "POST /service/r2/addoutput.jsp  HTTP/1.1\r\nHost: www.pvoutput.org\r\nX-Pvoutput-SystemId: %s\r\nX-Pvoutput-Apikey: %s\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: %d\r\n\r\n%s", systemId, apikey, (int)strlen(data), data);
	
	// DIT WERKT NIET
	//data="d=20121018,t=10:00,v1=1000,v2=150";
	//sprintf( strPOST, "POST /service/r2/addstatus.jsp  HTTP/1.1\r\nHost: www.pvoutput.org\r\nX-Pvoutput-SystemId: %s\r\nX-Pvoutput-Apikey: %s\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: %d\r\n\r\n%s", systemId, apikey, (int)strlen(data), data);
	
	// DIT WERKT
	//data="data=20121018,10:15,850,1109,-1,-1,23.1,240";
	sprintf( strPOST, "POST /service/r2/addbatchstatus.jsp  HTTP/1.1\r\nHost: www.pvoutput.org\r\nX-Pvoutput-SystemId: %s\r\nX-Pvoutput-Apikey: %s\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: %d\r\n\r\n%s", systemId, apikey, (int)strlen(data), data);
	
	
	UARTWrite(1, "\n\n");
	UARTWrite(1, strPOST);
	UARTWrite(1, "\n\n");
	


	while(flagTCP==FALSE)
	{
		if(z==5)
		{
			z=0;
			flagTCP=TRUE;
			error=1;
		}
		else
		{
			Socket=TCPClientOpen ( PVOutputHost, "80");
			vTaskDelay(25);
			flagTCP=TCPisConn(Socket);
			flagTCPisCON=flagTCP;
			vTaskDelay(25);
			z++;
		}
	}

	if(flagTCPisCON==TRUE)
	{
		TCPWrite ( Socket, strPOST, (int)strlen(strPOST) );
		vTaskDelay(200);
		TCPClientClose ( Socket );
		flagTCPisCON=FALSE;
	}

	return error;
}

